//
//  HouseDetailContainerViewController.m
//  Utils
//
//  Created by enghou on 2018/6/29.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "HouseDetailContainerViewController.h"
#import "SlidePageView.h"
#import "MemberListViewController.h"
#import "RealHouseDetailViewController.h"
#import "MembersItemModel.h"
@interface HouseDetailContainerViewController ()<ChangePage,UIPageViewControllerDelegate,UIPageViewControllerDataSource>

@property(nonatomic,strong)UIPageViewController *page;

@property(nonatomic,strong)NSArray *ctlArray;

@property(nonatomic,strong)SlidePageView *slide;
@end

@implementation HouseDetailContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pop) name:@"refresh_real_house" object:nil];
    
    UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delRecord)];
    trash.tintColor = [UIColor whiteColor];
    UIBarButtonItem *edit = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(updateRecord)];
    edit.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItems = @[trash,edit];
    
    self.slide = [[SlidePageView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    self.slide.backgroundColor = [UIColor whiteColor];
    [self.slide configTitles:@[@"基本信息",@"住房成员"]];
    self.slide.delegate = self;
    [self.view addSubview:self.slide];
    
    self.page = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.page.delegate = self;
    self.page.dataSource = self;
    
    NSMutableArray *array = [NSMutableArray array];
    RealHouseDetailViewController *detail = [[RealHouseDetailViewController alloc]init];
    detail.ids = self.ids;
    [array addObject:detail];
    MemberListViewController *list = [[MemberListViewController alloc]init];
    list.ids = self.ids;
    [array addObject:list];
    self.ctlArray = array;
    [self.page setViewControllers:@[self.ctlArray[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:^(BOOL finished) {

    }];
    [self.view addSubview:self.page.view];
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.page.view.frame = CGRectMake(0, 40, self.view.width, self.view.height - 40);
}

-(void)loadData{
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/houseInfo/houseInfoDetail.action" parameter:@{@"id":self.ids} completeBlock:^(NSDictionary *source) {
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source];
        if (model.success) {
            RealHouseDetailViewController *real = self.ctlArray[0];
            NSLog(@"%@",real.view);
            NSDictionary *data = source[@"data"];
            real.houseLocateDetailField.text = data[@"address"];
            real.zhuangField.text = data[@"block"];
            real.danyuanField.text = data[@"unit"];
            real.houseNumField.text = data[@"room"];
            real.xiaoquField.text = data[@"community"];
            real.houseUsageModel = [PropertyDictItemModel new];
            real.houseUsageModel.ids = data[@"houseUses"][@"id"];
            real.houseUsageField.text = data[@"houseUses"][@"displayName"];
            real.buildingAreaField.text = [NSString stringWithFormat:@"%.2f",[data[@"houseArea"] doubleValue]];
            real.cardModel = [PropertyDictItemModel new];
            real.cardModel.ids = data[@"certificateType"][@"id"];
            real.cardTypeField.text = data[@"certificateType"][@"displayName"];
            real.cardNumberField.text = data[@"certificateNumbe"];
            real.manipulatorNameField.text = data[@"name"];
            real.manipulatorMobileField.text = data[@"propertyPersonMobile"];
            real.manipulatorTelField.text = data[@"propertyPersonTel"];
            real.manipulatorNowLocateField.text = data[@"nowLiveAddress"];
            NSString *rental = data[@"rentalHouse"];
            if ([rental isEqualToString:@"是"]) {
                real.isRentHouseBtn.selected = YES;
                real.notRentHouseBtn.selected = NO;
            }else{
                real.isRentHouseBtn.selected = NO;
                real.notRentHouseBtn.selected = YES;
            }
            NSDictionary *addressType = data[@"addressType"];
            real.selectedHouseTypeId = addressType[@"id"];
            if ([addressType[@"displayName"] isEqualToString:@"商品房"]) {
                real.isShopHouseBtn.selected = YES;
                real.isOtherHouseBtn.selected = NO;
            }else{
                real.isShopHouseBtn.selected = NO;
                real.isOtherHouseBtn.selected = YES;
            }
            [real.tableView reloadData];
            NSMutableArray *source = [NSMutableArray array];
            NSArray *houseInfo = data[@"housePopulationInfo"];
            for (NSDictionary *element in houseInfo) {
                MembersItemModel *model = [[MembersItemModel alloc]initWithSource:element];
                [source addObject:model];
            }
            MemberListViewController *list = [self.ctlArray objectAtIndex:1];
            NSLog(@"%@",list.view);
            list.source = source;
            [list.tableView reloadData];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)delRecord{
    RealHouseDetailViewController *house = self.ctlArray[0];
    [house performSelector:@selector(delRecord)];
}

-(void)updateRecord{
    RealHouseDetailViewController *house = self.ctlArray[0];
    [house performSelector:@selector(updateRecord)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)changeToPage:(NSInteger)idx{
    NSInteger currentIdx = [_ctlArray indexOfObject:_page.viewControllers[0]];
    if ([_ctlArray count] > idx) {
        __weak HouseDetailContainerViewController *wself = self;
        if (idx > currentIdx) {
            [_page setViewControllers:@[_ctlArray[idx]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
                [wself.slide unlockForRespondsToScroll];
            }];
        }else if (idx < currentIdx){
            [_page setViewControllers:@[_ctlArray[idx]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {
                [wself.slide unlockForRespondsToScroll];
            }];
        }
    }
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger idx = [_ctlArray indexOfObject:viewController];
    if (idx == [_ctlArray count] - 1) {
        return viewController;
    }else{
        return [_ctlArray objectAtIndex:idx+1];
    }
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger idx = [_ctlArray indexOfObject:viewController];
    if (idx == 0) {
        return viewController;
    }else{
        return [_ctlArray objectAtIndex:idx - 1];
    }
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    if (finished) {
        if (completed) {
            NSInteger idx = [_ctlArray indexOfObject:pageViewController.viewControllers[0]];
            [self.slide slideToPosition:idx];
            [self.slide unlockForRespondsToScroll];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
